import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import departmentsAsync from '@/router/departments'
import settingsAsync from '@/router/settings'
import employeesAsync from '@/router/employees'
import permissionsAsync from '@/router/permissions'
import salarys from '@/router/salarys'
import socialSecuritys from '@/router/social_securitys'
import attendances from '@/router/attendances'
import approvals from '@/router/approvals'

// 动态路由表
export const asyncRoutes = [
  departmentsAsync,
  settingsAsync,
  employeesAsync,
  permissionsAsync,
  salarys,
  socialSecuritys,
  attendances,
  approvals
]

// 静态路由表
export const constantRoutes = [
  {
    path: '/import',
    component: () => import(/* webpackChunkName:"import"*/ '@/views/import/index')
  },

  {
    path: '/demo',
    component: () => import(/* webpackChunkName:"demo"*/ '@/views/demo/index')
  },

  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/ '@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import(/* webpackChunkName:"404"*/ '@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 指定路由规则,合并静态路由和动态路由生成最终路由表
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
